package com.surfmanager.dao;

import com.surfmanager.model.Profile;
import com.surfmanager.model.result.CodeType;
import com.surfmanager.util.PasswordEncrypt;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ProfileDAO {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@Transactional
	public CodeType save(Profile profile) {
		
          SessionFactory sf;
        Session sesion;
        Transaction tx=null;
        try{
            sf = HibernateUtil.getSessionFactory();
            sesion = sf.openSession();
            tx=sesion.beginTransaction();
            profile.setPassword(PasswordEncrypt.encrypt(profile.getPassword(), "SHA", "UTF-8"));
            CodeType code = checkProfile(profile.getUserName(), sf, sesion);
            if(code.equals(CodeType.OK)){
                sesion.save(profile);
            }
            tx.commit();
            sesion.close();
            return code;
        }catch(Exception ex){
            //System.err.println("error:"+ex.getStackTrace()+"\n"+ex.getMessage()+"\n"+ex.getCause());
            tx.rollback();
            return CodeType.KO_UNDEFINED;
            
        }
	}
	
	@Transactional
	public List<Profile> list() {
		 SessionFactory  sf = HibernateUtil.getSessionFactory();
           Session sesion = sf.openSession();
           Query query = sesion.createQuery("from Profile");
           List<Profile> profiles = query.list();
           sesion.close();
           return profiles;
	}
	
        
         public Profile getProfile(String userName){
       
             try{
          SessionFactory  sf = HibernateUtil.getSessionFactory();
           Session sesion = sf.openSession();
           //Transaction tx=sesion.beginTransaction();
            Query query = sesion.createQuery("from Profile P where P.userName = :username");
            query.setParameter("username",userName);
           List<Profile> profiles = query.list();
            sesion.close();
            if((profiles!=null) &&(!profiles.isEmpty())){
                System.out.println("Amine: devuelto profile");
                return profiles.get(0);
            }else{
                return null;
            }
             }catch(Exception ex){
                   System.err.println("error Amine:"+ex.getStackTrace()+"\n"+ex.getMessage()+"\n"+ex.getCause());
             }
             return null;
        }
         
          public CodeType checkProfile(String userName, SessionFactory  sf , Session sesion){
       
        Query query = sesion.createQuery("from Profile P where P.userName = :username");
            query.setParameter("username",userName);
           List<Profile> profiles = query.list();
           if((profiles!=null) && (profiles.size()!=0)){
                return CodeType.KO_DUPLICATED_PROFILE;
            }else{
                return CodeType.OK;
            }
        }
         
        
        
	@Transactional
	public Profile get(Integer id) {
		Session session = sessionFactory.getCurrentSession();
		return (Profile)session.get(Profile.class, id);
	}

	@Transactional
	public void remove(Integer id) {
		Session session = sessionFactory.getCurrentSession();
		
		Profile user = (Profile)session.get(Profile.class, id);
		
		session.delete(user);
	}
        
        
}
